﻿/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#ifndef SHARED_GLOBAL_MACROS_H_
#define SHARED_GLOBAL_MACROS_H_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#include <cassert>
#include <cstdio>

///////////////////////////////////////////////////////////////////////////////
/* 大小端 */
#define SHARED_LITTLEENDIAN 0
#define SHARED_BIGENDIAN    1


///////////////////////////////////////////////////////////////////////////////
#define SHARED_MAX(a, b)    ( (a) > (b) ? (a) : (b) )
#define SHARED_MIN(a, b)    ( (a) < (b) ? (a) : (b) )


///////////////////////////////////////////////////////////////////////////////
#define DISALLOW_COPY_AND_ASSIGN(TypeName)  \
    TypeName(const TypeName&);              \
    void operator=(const TypeName&)


///////////////////////////////////////////////////////////////////////////////
#define SHARED_ASSERT(expr)                                     \
    if (!(expr)) {												\
        /*printf("Assert failed, %s, %d\n", __FILE__, __LINE__);*/	\
        assert(false);											\
    }															\


///////////////////////////////////////////////////////////////////////////////
#define SHARED_TYPE_LOCK_GURAD(Type, Var)                                  \
    LockGuard<Type> guard##Var(Var);                                       \

#define SHARED_TYPE_LOCK_GURAD_ASSERT(Type, Var)                           \
    LockGuard<Type> guard##Var(Var);                                       \
    if (!guard##Var.locked()) {                                            \
        SHARED_ASSERT(false);                                              \
    }                                                                      \

#define SHARED_TYPE_LOCK_GURAD_ASSERT_RETURN(Type, Var)                    \
    LockGuard<Type> guard##Var(Var);                                       \
    if (!guard##Var.locked()) {                                            \
        SHARED_ASSERT(false);                                              \
        return;                                                            \
    }                                                                      \

#define SHARED_TYPE_LOCK_GURAD_ASSERT_RETURN_VAR(Type, Var, Rtn)           \
    LockGuard<Type> guard##Var(Var);                                       \
    if (!guard##Var.locked()) {                                            \
        SHARED_ASSERT(false);                                              \
        return Rtn;                                                        \
    }                                                                      \


///////////////////////////////////////////////////////////////////////////////
#define SHARED_LOCK_GURAD(Var)                          \
    SHARED_TYPE_LOCK_GURAD(Mutex, Var)

#define SHARED_LOCK_GURAD_ASSERT(Var)                   \
    SHARED_TYPE_LOCK_GURAD_ASSERT(Mutex, Var)

#define SHARED_LOCK_GURAD_ASSERT_RETURN(Var)            \
    SHARED_TYPE_LOCK_GURAD_ASSERT_RETURN(Mutex, Var)

#define SHARED_LOCK_GURAD_ASSERT_RETURN_VAR(Var, Rtn)   \
    SHARED_TYPE_LOCK_GURAD_ASSERT_RETURN_VAR(Mutex, Var, Rtn)


#endif // SHARED_GLOBAL_MACROS_H_
